//
//  StrategyDataBase.m
//  GiftItems
//
//  Created by lanou3g on 15/9/23.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "StrategyDataBase.h"

@implementation StrategyDataBase
// 创建单例类

static StrategyDataBase *strategyDataBase;
+ (StrategyDataBase *)shareDataBase {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        strategyDataBase = [[StrategyDataBase alloc] init];
    });
    return strategyDataBase;
}

//创建fmdb
- (FMDatabase *)dataBase {
    
    if (!_dataBase) {
        _dataBase = [[FMDatabase alloc] init];
    }
    return _dataBase;
}

// 指定数据库路径
- (NSString *)sqlitePaths:(NSString *)sqlitePaths {
    NSString *str = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:sqlitePaths];
    NSLog(@"%@",str);
    return str;
}
// 打开数据库
- (void)openSqliteWithPaths:(NSString *)paths {
    self.dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:paths]];
}
// 关闭数据库
- (void)closeSqlite {
    [self.dataBase close];
}
// 建表
- (void)createStrategyListWithName:(NSString *)name {
    if(!_dataBase) {
        [self openSqliteWithPaths:[NSString stringWithFormat:@"%@.db",name]];
    }
    if ([_dataBase open]) {
        BOOL executeUpdata = [_dataBase executeUpdate:@"CREATE TABLE strategyList(ID Integer PRIMARY KEY,cover_image_url TEXT,title TEXT)"];
        if (executeUpdata) {
            NSLog(@"建表成功");
        }else {
            NSLog(@"建表失败");
        }
    }
}
// 插入一条数据
- (void)insertModel:(MainBoutiqueModel *)model {
    if (!model) {
        NSLog(@"model为空");
        return;
    }
    if (!_dataBase) {
        _dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:@"strategyList.db"]];
    }
    if ([_dataBase open]) {
        NSString *sql = [NSString stringWithFormat:@"insert into strategyList(ID,title,cover_image_url) values(?,?,?)"];
        BOOL inserModel = [_dataBase executeUpdate:sql,model.ID,model.title,model.cover_image_url];
        if (inserModel) {
            NSLog(@"插入成功");
        }else {
            NSLog(@"插入失败");
        }
    }
    
}
// 获取所有数据
- (NSMutableArray *)selegetAll {
    NSMutableArray *modelArray = [[NSMutableArray alloc] init];
    if (!_dataBase) {
        _dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:@"strategyList.db"]];
    }if ([_dataBase open]) {
        NSString *sql = @"select *from strategyList";
        FMResultSet *rs = [_dataBase executeQuery:sql];
        
        while ([rs next]) {
            MainBoutiqueModel *model = [[MainBoutiqueModel alloc] init];
            model.title = [rs stringForColumn:@"title"];
            model.cover_image_url = [rs stringForColumn:@"cover_image_url"];
            model.ID = [rs stringForColumn:@"ID"];
            [modelArray addObject:model];
        }
    }
    return modelArray;
}
// 删除一条信息
- (void)deleteModelWithStrategy:(NSString *)title {
    if (!_dataBase) {
        _dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:@"strategyList.db"]];
    }
    if ([_dataBase open]) {
        NSString *sql = [NSString stringWithFormat:@"delete from strategyList where title = ?"];
        BOOL b = [_dataBase executeUpdate:sql,[NSString stringWithFormat:@"%@",title]];
        if (b) {
            NSLog(@"删除成功");
        }else {
            NSLog(@"删除失败");
        }
    }
}


// 根据信息查找相对应的model
- (NSArray *)findStrategyModelByTitle:(NSString *)title {
    NSMutableArray *modelArray = [[NSMutableArray alloc] init];
    if (!_dataBase) {
        _dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:@"strategyList.db"]];
    }
    if ([_dataBase open]) {
        NSString *sql = [NSString stringWithFormat:@"select *from strategyList where title = ?"];
        FMResultSet *rs = [_dataBase executeQuery:sql,[NSString stringWithFormat:@"%@",title]];
        
        while ([rs next]) {
            MainBoutiqueModel *model = [[MainBoutiqueModel alloc] init];
            model.title = [rs stringForColumn:@"title"];
            model.cover_image_url = [rs stringForColumn:@"cover_image_url"];
            model.ID = [rs stringForColumn:@"ID"];
            [modelArray addObject:model];
        }

    }
    
    return modelArray;
    
}


//添加 收藏提醒框图
- (void)showAlert {
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"收藏" message:@"收藏成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    [alert show];
    
    [self performSelector:@selector(dimissAlert:) withObject:alert afterDelay:0.5];
    
}
//取消 收藏提醒框图
- (void)deleteAlert {
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"收藏" message:@"取消收藏" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    [alert show];
    [self performSelector:@selector(dimissAlert:) withObject:alert afterDelay:0.5];
}

// 提醒视图消失
- (void)dimissAlert:(UIAlertView *)alert {
    
    if (alert) {
        [alert dismissWithClickedButtonIndex:[alert cancelButtonIndex] animated:YES];
    }
}










@end
